/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.htk.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.htk.entity.HtkNews;
import com.thinkgem.jeesite.modules.htk.service.HtkNewsService;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 每日资讯信息Controller
 * @author htk
 * @version 2018-04-26
 */
@Controller
@RequestMapping(value = "${adminPath}/htk/htkNews")
public class HtkNewsController extends BaseController {

	@Autowired
	private HtkNewsService htkNewsService;
	
	@ModelAttribute
	public HtkNews get(@RequestParam(required=false) String id) {
		HtkNews entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = htkNewsService.get(id);
		}
		if (entity == null){
			entity = new HtkNews();
		}
		return entity;
	}
	
	@RequiresPermissions("htk:htkNews:view")
	@RequestMapping(value = {"list", ""})
	public String list(HtkNews htkNews, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HtkNews> page = htkNewsService.findPage(new Page<HtkNews>(request, response), htkNews); 
		model.addAttribute("page", page);
		return "modules/htk/htkNewsList";
	}

	@RequiresPermissions("htk:htkNews:view")
	@RequestMapping(value = "form")
	public String form(HtkNews htkNews, Model model) {
		model.addAttribute("htkNews", htkNews);
		return "modules/htk/htkNewsForm";
	}

	@RequiresPermissions("htk:htkNews:edit")
	@RequestMapping(value = "save")
	public String save(HtkNews htkNews, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, htkNews)){
			return form(htkNews, model);
		}
		htkNewsService.save(htkNews);
		addMessage(redirectAttributes, "保存资讯成功");
		return "redirect:"+Global.getAdminPath()+"/htk/htkNews/?repage";
	}
	
	@RequiresPermissions("htk:htkNews:edit")
	@RequestMapping(value = "delete")
	public String delete(HtkNews htkNews, RedirectAttributes redirectAttributes) {
		htkNewsService.delete(htkNews);
		addMessage(redirectAttributes, "删除资讯成功");
		return "redirect:"+Global.getAdminPath()+"/htk/htkNews/?repage";
	}
	@ResponseBody
	@RequestMapping(value = "/api/newsData")
	public List<Map<String, Object>> treeData( HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<HtkNews> list = htkNewsService.findList(new HtkNews());
		for (int i=0; i<list.size(); i++){
			HtkNews e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", "u_"+e.getId());
			map.put("title", e.getTitle());
			map.put("content", e.getContent());
			map.put("times", e.getTimes());
			map.put("url", e.getUrl());
			map.put("type", e.getType());
			mapList.add(map);
		}
		return mapList;
	}

}